package com.caxxb.controller;

import com.caxxb.common.R;
import com.caxxb.entity.Game;
import com.caxxb.entity.Order;
import com.caxxb.entity.ShoppingCart;
import com.caxxb.service.GameService;
import com.caxxb.service.OrderService;
import com.caxxb.service.ShoppingCatService;
import com.caxxb.service.impl.GameServiceImpl;
import com.caxxb.service.impl.OrderServiceImpl;
import com.caxxb.service.impl.ShoppingCartServiceImpl;
import com.caxxb.util.RandomCode;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("shopping")
public class ShoppingCartController {

    private OrderService orderService=new OrderServiceImpl();

    private GameService gameService=new GameServiceImpl();

    private ShoppingCatService shoppingCatService=new ShoppingCartServiceImpl();

    //增加到购物车的方法
    @PostMapping("add/{gameid}")
    public R addToCart(@PathVariable int gameid, HttpSession session){
        //从session获取用户id
        int userid = (int) session.getAttribute("user");

        //设置是否添加成功的标识
        boolean flag=false;
        //查询是否购买过游戏
        boolean checkOrder=true;

        System.out.println("userid:"+userid);
        System.out.println("gameid:"+gameid);
        //通过游戏id获取游戏价格
        List<Game> gameById = gameService.getGameById(gameid);
        Game game = gameById.get(0);
        Double gamePrice = game.getPrice();

        //通过id查看是否存在购物车
        List cartById = shoppingCatService.getCartById(userid, gameid);
        int size = cartById.size();

//        System.out.println("集合大小:"+size);
        //如果不存在
        if(size==0){

            //查看是否购买过游戏
            List<Order> orders = orderService.CheckOrderByUserId(userid, gameid);
            for (Order order : orders) {
                int game_id = order.getGame_id();

                if (game_id==gameid){
                    checkOrder=false;
                    return R.error("已经购买过游戏");
                }
            }

             if(checkOrder){
                 //添加购物车
//                 System.out.println("运行此代码");
                 flag = shoppingCatService.addToCart(gameid, userid, gamePrice);
//                 System.out.println("增加进购物车成功"+flag);
             }


        }

        if (flag){
            return R.success(flag);
        }
            return R.error("已经在购物车里");


    }

    //根据用户id显示购物车的方法
    @GetMapping("show")
    public R showCarts(HttpSession session){
        //从session获取用户id
        int userid = (int) session.getAttribute("user");
//        System.out.println("userid:"+userid);
        //获取购物车
        List list = shoppingCatService.getCartsByUserId(userid);
        System.out.println(list);
        return R.success(list);
    }

    //根据id删除购物车的方法
    @DeleteMapping("del/{cartid}")
    public R del(@PathVariable int cartid){

//        System.out.println("cartid:"+cartid);

        boolean flag = shoppingCatService.delCartByCartId(cartid);

        if(flag){
            return R.success(flag);
        }
        return R.error("删除失败");
    }

    //清空购物车的方法
    @PostMapping("clear")
    public R clear(@RequestParam(value ="ids[]" ) Integer[] ids,HttpSession session){

        //从session获取用户id
        int userid = (int) session.getAttribute("user");


        boolean flag=false;

        //获取到选中的游戏id
        for (Integer gameid : ids) {
            System.out.println("游戏id:"+gameid);
            System.out.println("用户id:"+userid);

            //设置是否删除成功的标识
            flag = shoppingCatService.delCartById(userid, gameid);

            //通过游戏id获取游戏
            List<Game> gameById = gameService.getGameById(gameid);
            Game game = gameById.get(0);

            //如果购买成功，增加游戏销量
            if (flag){
                //增加游戏销量
                gameService.addSales(gameid);

                //生成随机兑换码
                String stringRandom = RandomCode.getStringRandom(20);
                //往订单插入数据
                orderService.addToOrder(game,userid,stringRandom);
            }

        }

        if(flag) {
            return R.success(ids);
        }

        return R.error("清空购物车失败");
    }


}
